<?php

include_once('staff.php');

/**
 * This class provides dynamic data (autocomplete, etc) for the staff pages.
 * @package staff
 */
class jquery extends staff
{
	/**
     * Returns a json encoded list of variables from specified table and field based on input from $_POST('term')
	 * @param $table_name Name of table
	 * @param $field_name Name of field 
     * @return string
     */	
	function autocomplete( $table_name, $field_name )
	{
		// Search term from jQuery
		$term = $this->input->post('term');
			
		$this->load->model('jquery_model');			
		$rows = $this->jquery_model->getAutocomplete($table_name, $field_name, $term);
			
		$keywords = array();
		foreach ($rows as $row)
			array_push($keywords, $row->$field_name);

		echo json_encode($keywords);		
	}

	/**
     * Returns a string containing details about a volume (access_id acquired from $_POST('access_id'))
     * @return string
     */	
	function getItemDetails( )
	{
		// Search term from jQuery
		$access_id = $this->input->post('access_id');
		
		$this->load->model('volume_model');
		$volume = $this->volume_model->getVolumeByID( $access_id )->row();
		
		// If Volume found and it's not on loan, echo it's details
		if ($volume)
			echo "Title: " . $volume->title;
	}

	/**
     * Returns a string containing information about a loan (access_id acquired from $_POST('access_id'))
     * @return string
     */	
	function getLoanDetails( )
	{
		$this->load->model('loan_model');
		
		$loan = $this->loan_model->getLoanByID( $this->input->post('access_id') )->row();
		
		if ($loan)
		{
			$this->load->model('volume_model');
			$this->load->model('subscriber_model');
			$volume = $this->volume_model->getVolumeByID($loan->access_id)->row();
			$subscriber = $this->subscriber_model->getSubscriberByID($loan->subscriber_id)->row();
			
			echo	'<b>Title:</b> ' . $volume->title . '<br/>' .
					'<b>Loaned By:</b> ' . $subscriber->first_name . ' ' . $subscriber->last_name . '<br/>' .
					'<b>Loan Date:</b> ' . $loan->loan_date . '<br/>' .
					'<b>Due Date:</b> ' . $loan->due_date . '<br/>';
			
			if ($loan->reserve_subscriber)
			{
				$res_sub = $this->subscriber_model->getSubscriberByID($loan->reserve_subscriber)->row();
				echo '<b>Reserve Subscriber:</b> ' . $res_sub->first_name . ' ' . $res_sub->last_name;
			}
			
			else
				echo '<b>Reserve Subscriber:</b> None';
		}
	}

    /**
     * Returns a list of subscribers matching search term ($_POST('term')) for adding a loan
     * @param string $method Can search by either 'subscriber_id' or 'last_name' 
     * @return string
     */	
	function getSubscriberDetailsForAddLoan( $method )
	{
		$this->load->model('subscriber_model');

		if ( $method == 'subscriber_id' )
			$subscribers = $this->subscriber_model->getSubscriberByID( $this->input->post('subscriber_id') );
		
		else if ( $method == 'last_name' )
			$subscribers = $this->subscriber_model->getSubscribersByLastName( $this->input->post('last_name') );

		echo 'Please select a subscriber from the list below: <br><br>';
			
		foreach ($subscribers->result() as $subscriber)
			echo  anchor('staff/loan_management/addLoan/' . $subscriber->subscriber_id, $subscriber->subscriber_id . ', ' . $subscriber->first_name . ' ' . $subscriber->last_name . ', ' . $subscriber->suburb . '<br/>');
	}
	
    /**
     * Returns a list of subscribers matching search term ($_POST('term')) for adding a payment
     * @param string $method Can search by either 'subscriber_id' or 'last_name' 
     * @return string
     */	
	function getSubscriberDetailsForPayment( $method )
	{
		$this->load->model('subscriber_model');

		if ( $method == 'subscriber_id' )
			$subscribers = $this->subscriber_model->getSubscriberByID( $this->input->post('subscriber_id') );
		
		else if ( $method == 'last_name' )
			$subscribers = $this->subscriber_model->getSubscribersByLastName( $this->input->post('last_name') );

		echo 'Please select a subscriber from the list below: <br><br>';
			
		foreach ($subscribers->result() as $subscriber)
			echo  anchor('staff/subscriber_management/addPayment/' . $subscriber->subscriber_id, $subscriber->subscriber_id . ', ' . $subscriber->first_name . ' ' . $subscriber->last_name . ', ' . $subscriber->suburb . '<br/>');
	}

    /**
     * Returns a list of subscribers matching search term ($_POST('term')) for reserving loan
     * @param string $method Can search by either 'subscriber_id' or 'last_name' 
	 * @param integer $access_id Accession Number of volume to reserve
     * @return string
     */	
	function getItemDetailsForReserveLoan( $method )
	{
		$this->load->model('volume_model');
		$this->load->model('loan_model');
		
		if ( $method == 'access_id' )
			$volumes = $this->volume_model->getVolumeByID( $this->input->post('access_id') );
		
		else if ( $method == 'title' )
			$volumes = $this->volume_model->getSearchResults( 'title', $this->input->post('title'), 0, 20 );
		
		echo 'Please select an item from the list below: <br><br>';
			
		foreach ($volumes->result() as $volume)
		{
			if ( $this->loan_model->getLoanByID($volume->access_id)->row() )
				echo anchor('staff/loan_management/reserveLoan/' . $volume->access_id, $volume->access_id . ', ' . $volume->title) . '<br/>';
			
			else
				echo $volume->access_id . ', ' . $volume->title . ' (Item not on loan)<br/>';
		}
	}
	
    /**
     * Returns a list of subscribers matching search term ($_POST('term')) for reserving loan
     * @param string $method Can search by either 'subscriber_id' or 'last_name' 
	 * @param integer $access_id Accession Number of volume to reserve
     * @return string
     */	
	function getSubscriberDetailsForReserveLoan( $method, $access_id )
	{
		$this->load->model('subscriber_model');

		if ( $method == 'subscriber_id' )
			$subscribers = $this->subscriber_model->getSubscriberByID( $this->input->post('subscriber_id') );
		
		else if ( $method == 'last_name' )
			$subscribers = $this->subscriber_model->getSubscribersByLastName( $this->input->post('last_name') );

		echo 'Please select a subscriber from the list below: <br><br>';
			
		foreach ($subscribers->result() as $subscriber)
			echo  anchor('staff/loan_management/reserveLoan/' . $access_id . '/' . $subscriber->subscriber_id, $subscriber->subscriber_id . ', ' . $subscriber->first_name . ' ' . $subscriber->last_name . ', ' . $subscriber->suburb . '<br/>');
	}
}

?>